<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\UserRepo;
use App\Repos\UserCouponRepo;

class UserController extends Controller
{
    public function index(Request $request)
    {
    	$param = [];
        if($request->keyword) {
            $param['keyword'] = trim($request->keyword);
        }
        $list = UserRepo::getList($param);
        //var_dump($param);die;
    	return view('admin.user.index',compact('list','param')); 
    }

    public function edit($user_id)
    {
        $data = UserRepo::find(['id'=>$user_id]);
        return view('admin.user.edit',compact('data'));
    }

    public function update(Request $request)
    {
        
        $rs['status'] = 'danger';
        $rs['msg']    = '操作失败';
        $data = $request->except('_token');
        $id   = array_pull($data,'id');
        $flag = UserRepo::update(['id'=>$id],$data);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
        }
        return back()->with('rs',$rs);
    }

    /**
     * 改变用户的状态 1启用 0停用
     */
    public function changeStatus(Request $request)
    {
        $rs['status'] = 'danger';
        $rs['msg']    = '操作失败';
        $id    = (int)$request->id;
        $data = $request->except(['_token']);
       
        $flag = UserRepo::changeStatus($id,$data);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
        }
        return back()->with('rs',$rs);
    }

    public function coordinate($user_id)
    {
    	$list = UserRepo::getCoordinate($user_id);
    	return view('admin.user.coordinate',compact('list'));
    }

    public function address($user_id)
    {
    	$list = UserRepo::getAddress($user_id);
    	return view('admin.user.address',compact('list'));
    }

    public function envelope($user_id)
    {
    	$list =  UserRepo::getEnvelope($user_id);
    	return view('admin.user.envelope',compact('list'));
    }

    public function moneylog($user_id)
    {
        $user = UserRepo::find(['id'=>$user_id]);
        $list = UserRepo::getUserMoneyLog($user_id);
        return view('admin.user.moneylog',compact('list','user'));
    }

    public function coupon($user_id)
    {
        $user = UserRepo::find(['id'=>$user_id]);
        $where['user_id'] = $user_id;
        $list = UserCouponRepo::getPages($where);
        $where['is_used'] = 0;
        $unUsed = UserCouponRepo::countNum($where);
        $where['is_used'] = 1;
        $used = UserCouponRepo::countNum($where);
        return view('admin.user.coupon',compact('list','user','unUsed','used'));
    }

    
}
